import {
  postRequest,
  postRequestNoToken,
  getRequest,
  getRequestNoToken
} from  './exception'
import axios from 'axios'
import { apiUrl as url } from '../url'
import jsonp from 'jsonp'

export function getPosition(callback) {
  jsonp('http://api.map.baidu.com/location/ip?ak=PKjiNSEtPtxphfaUacba5mieByhERV6x&coor=bd09ll', null, callback)
}

export function userRegister(data) {
  return getRequestNoToken('/api/user/register', data)
}

export function getVerifyCodeImage() {
  return axios.get(url + '/api/user/getVerifyCodeImage', { responseType: 'blob' })
}
export function verifyCode(verifyCode) {
  return getRequestNoToken('/api/user/verifyCode', verifyCode)
}

export function registerVerify(data) {
  return getRequestNoToken('/api/user/registerVerify', data)
}
export function userVerify(data) {
  return getRequestNoToken('/api/user/verifySuccess', data)
}

export function forgetPass(data) {
  return postRequestNoToken('/api/user/retrievePassword', data)
}

export function resetPass(data) {
  return postRequestNoToken('/api/user/restPassword', data)
}

export function userLogin(data) {
  return getRequestNoToken('/api/user/login', data)
}

export function getToken() {
  const state = sessionStorage.getItem('state')
  const { token } = JSON.parse(state)
  return token
}
export function getUserToken() {
  const state = sessionStorage.getItem('state')
  try {
    const { token } = JSON.parse(state)
    return token
  } catch (error) {
    return
  }

}
export function getUserInfo() {
  return JSON.parse(sessionStorage.getItem('state'))
}

export function getUserName() {
  return getUserInfo().username
}

export function getUserEmail() {
  return getUserInfo().email
}

export function getUserPhone() {
  return getUserInfo().phone
}

export function getUserBasicInfo() {
  const { username, name, address, companyName, companyLogo } = getUserInfo()

  return { username, name, address, companyName, companyLogo }
}

export function getUserInfo2() {
  return getRequest('/api/user/getUserInfo')
}

export function farmLandSave(data) {
  return postRequest('/api/farmLand/save', data)
}

export function farmLandModify(data) {
  return getRequest('/api/farmLand/save',data)
}

export function findFarmers() {
  return getRequest('/api/user/findFarmers')
}

export function getFarmers(data) {
  return postRequest('/api/user/getFarmers', data)
}

export function addFarmers(data) {
  return postRequest('/api/user/addFarmers', data)
}


export function updateFarmers(data) {
  return postRequest('/api/user/updateFarmers', data)
}

export function updateContact(data) {
  return postRequest('/api/user/updateContact', data)
}

export function findPlanDetails() {
  return postRequest('/api/user/planDetails', data)
}
export function updateContactSuccess(data) {
  return postRequest('/api/user/updateContactSuccess', data)
}
export function updateContactEmailSuccess(data, token) {
  return postRequestNoToken('/api/user/updateContactSuccess', data, {}, `token=${token}`)
}
export function findCriosAndVarietiesList() {
  return getRequestNoToken('/api/crops/findCriosAndVarietiesList')
}

export function findSoilList() {
  return getRequestNoToken('/api/crops/findAllList')
}

export function findPestsByCropsId(id) {
  var image = new FormData()
  image.append('cropsId', id)
  return postRequestNoToken('/api/diseasePests/findByCropsId', image)
}

export function findReasonById(data) {
  getRequest('/api/quarterCrops/findById', data)
}

export function saveSeasonInfo(landInfo) {
  return postRequest('/api/quarterCrops/saveInfo', landInfo)
}

export function updateIcon(ican) {
  return postRequest('/api/user/updateIcon', ican)
}

export function updatePassword(password) {
  return postRequest('/api/user/updatePassword', password)
}
export function updateUserInfo(userInfo) {
  return postRequest('/api/user/updateUserInfo', userInfo)
}


export function farmLandLogSave(farmLandLog, config) {
  return postRequest('/api/farmlandLog/save', farmLandLog, config)
}

export function farmLandLogDelete(logId) {
  return postRequest('/api/farmlandLog/delete', logId)
}

export function findSoilTypeList() {
  return getRequestNoToken('/api/soil/findAllList')
}

export function findLogPhotoList(params) {
  return postRequest('/api/logPhoto/findList', params)
}
export function findlandLogList(params) {
  return postRequest('/api/farmlandLog/findList', params)
}
export function getUserIcon() {
  return `${url}/api/user/downLoadUserIcon?token=${getToken()}&timestemp=${Date.now()}`
}
export function findLogPhotoById(params) {
  return postRequest('/api/logPhoto/findByLogId', params)
}
export function findLogVideoById(params) {
  return postRequest('/api/logVideo/findByLogId', params)
}
export function deleteLogPhotoById(id) {
  return postRequest('/api/logPhoto/delete', id)
}
export function deleteLogVideoById(id) {
  return postRequest('/api/logVideo/delete', id)
}

export function findSeasonLists() {
  return postRequestNoToken('/api/season/findAllList')
}

export function findPlantingSeasonList(landId) {
  return postRequest('/api/plantingSeason/findList', landId)
}
// > api/plantingSeason/save

export function plantingSeasonSave(info) {
  return postRequest('/api/plantingSeason/save', info)
}
// 	api/plantingSeasonCrops/findList
export function findPlantingSeasonCrops(plantingSeasonId) {
  return postRequest('/api/plantingSeasonCrops/findList', plantingSeasonId)
}
// 	api/plantingSeasonCrops/save
export function plantingSeasonCropsSave(info) {
  return postRequest('/api/plantingSeasonCrops/save', info)
}
export function plantingSeasonCropsDelete(id) {
  return postRequest('/api/plantingSeasonCrops/delete', id)
}
export function setInSeason(info) {
  return postRequest('/api/plantingSeason/inSeason', info)
}
// > api/plantingSeason/delete

export function deleteSeason(info) {
  return postRequest('/api/plantingSeason/delete', info)

}
// > api/soilLand/save
export function soilLandSave(info) {
  return postRequest('/api/soilLand/save', info)
}
// > api/environmentFacilities/save

export function envirFacSave(info) {
  return postRequest('/api/environmentFacilities/save', info)
}

export function findDiseasePestList () {
  return postRequest('/api/diseasePests/findAllList')
}

export function findNaturalDisastersList() {
  return postRequest('/api/naturalDisasters/findList')
}
// 	api/croppingPattern/findByCropsId
export function findCroppingPatternList (cropsId) {
  return postRequest('/api/croppingPattern/findByCropsId', cropsId)
}

// 	api/soilLand/findByLandId
export function findsoilLandList (landId) {
  return postRequest('/api/soilLand/findByLandId', landId)
}
// api/soilLand/delete?token=&id=
export function deleteSoilLand(soilLand) {
  return postRequest('/api/soilLand/delete', soilLand)
}
// 	api/environmentFacilities/findByLandId
export function findEnvirFac (landId) {
  return postRequest('/api/environmentFacilities/findByLandId', landId)
}
// 	api/soilLand/deleteFile
export function deleteSoilLandFile(info) {
  return postRequest('/api/soilLand/deleteFile', info)
}
// api/plantingSeasonCrops/delete?token=&id=

// api/plantingSeasonCropsFertilizer/delete?token=&id=
export function plantingSeasonCropDelete (id) {
  return postRequest('/api/plantingSeasonCropsFertilizer/delete', id)
}

// api/farmLand/getTotalArea

export function farmLandTotalArea() {
  return postRequestNoToken('/api/farmLand/getTotalArea')
}
// 	api/farmLand /updateSort
export function farmLandUpdateList(content) {
  return postRequest('/api/farmLand/updateSort', content)
}
// 	api/farmLand/findList
export function farmLandLists () {
  return postRequest('/api/farmLand/findList')
}
// 	api/user/validatePassword
export function validatePassword(password) {
  return postRequest('/api/user/validatePassword', password)
  
}
// 	api/plantingSeasonCropsFertilizer/save
export function fertilizerSave(fertilizerStr) {

  return postRequest('/api/plantingSeasonCropsFertilizer/save', fertilizerStr)
}
// 	api/plantingSeasonCropsFertilizer/get
export function getFertilizer(id) {
  return postRequest('/api/plantingSeasonCropsFertilizer/get', id)
}
// 	api/farmLand/isShow
export function setFarmLandShow(params) {
  return postRequest('/api/farmLand/isShow', params)

}

export function findSimple(farmlandId) {
  return postRequest('/api/plantingSeasonCrops/findSimpleByNowSeasonCrops', farmlandId)
}

// 	api / findByPlantingSeasonCropsId / save

export function findByPlantingSeasonCropsId(fd) {
  return postRequest('/api/plantingScheme/findByPlantingSeasonCropsId', fd)
}

export function plantingSchemeSave(fd) {
  return postRequest('/api/plantingScheme/save', fd)
}

// 	api/plantingScheme/getPdf
export function getPdf(fd) {
  return getRequest('/api/plantingScheme/getPdf', fd)
}

// 	api/weather/forecast
export function getWeatherForecast(landId) {
  return getRequest('/api/weather/forecast', landId)
}
// 	api/weather/history
export function getWeatherHistory(fd) {
  return getRequest('/api/weather/history', fd)
}

// 	api/weather/historicalSummary
export function getWeatherHistorySummary(fd) {
  return getRequest('/api/weather/historicalSummary', fd)
}

// 	api/farmlandWeather/findByFarmlandId

export function findFarmlandWeather(fd) {
  return getRequest('/api/farmlandWeather/findByFarmlandId', fd)
}
// /	api/farmlandWeather/update
export function updateFarmlandWeather(infoStr) {
  return postRequest('/api/farmlandWeather/update', infoStr)
}
// 	api/farmlandWeather/copy
export function copyFarmlandWeather(fd) {
  return postRequest('/api/farmlandWeather/copy', fd)
}

// 	api/farmlandWeather/getEchartData
export function getEchartData(fd) {
  return getRequest('/api/farmlandWeather/getEchartData', fd)
}

// 	api/farmLand/findAround
export function findAround(landId) {
  return getRequest('/api/farmLand/findAround', landId)
}


// 	api/weather/historySummaryDetails
export function historySummaryDetails(fd) {
  return getRequest('/api/weather/historySummaryDetails', fd)
}

//	api/opinion/findList
export function findListOpts(fd) {
  return postRequest('/api/opinion/findList', fd)
}

// 	api/opinion/save

export function saveOpts(fd) {
  return postRequest('/api/opinion/save', fd)
}

//	api/opinion/delete
export function deleteOpts(fd) {
  return postRequest('/api/opinion/delete', fd)
}


//	api/opinion/findList
export function findListDecs(fd) {
  return postRequest('/api/dicision/findList', fd)
}

// 	api/opinion/save

export function saveDecs(fd) {
  return postRequest('/api/dicision/save', fd)
}

//	api/opinion/delete
export function deleteDecs(fd) {
  return postRequest('/api/dicision/delete', fd)
}
// 	api/gduCoefficient/findByLandId
export function getGduCoefficient(fd) {
  return postRequest('/api/gduCoefficient/findByLandId', fd)
}

export function saveGduCoefficient(fd) {
  return postRequest('/api/gduCoefficient/save', fd)
}


// http://192.168.1.105:8080/ican_n/api/gadm/getLocation?longitude=106.209292&latitude=38.000347
export function getLocationByLonLat({ longitude, latitude }) {
  return getRequestNoToken('/api/gadm/getLocation', {
    longitude,
    latitude
  })
}